#!/bin/bash
#
set -e
folder=../
echo installing requires
sudo apt-get install -y apt-utils curl python3-testresources python3-dev python3-distutils \
                        libboost-python1.65.1 libcanberra-gtk-module \
                        mpg123 >/dev/null 2>&1
pushd $folder 1>/dev/null
echo installing pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py >/dev/null 2>&1
sudo -H python3 get-pip.py 1>/dev/null
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 1>/dev/null
{
    sudo mkdir -p /root/.config/pip/ >/dev/null 2>&1
}||{
    sudo cp ~/.config/pip/*.conf /root/.config/pip/
}
pip --version
echo installing numpy
sudo -H pip install numpy==1.16.5 >/dev/null 2>&1

echo installing pycuda
cd pycuda-2019.1.2
sudo -H python3 setup.py install >/dev/null 2>&1
cd ../
python3 -c "import pycuda;import pycuda.autoinit; print('pycuda version:', pycuda.VERSION)"


echo installing Jetson GPIO Module
sudo -H pip install Jetson.GPIO 1>/dev/null
sudo groupadd -f -r gpio
sudo usermod -a -G gpio shuaihu
sudo cp /usr/local/lib/python3.6/dist-packages/Jetson/GPIO/99-gpio.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules && sudo udevadm trigger


echo Installing pyserial
sudo -H pip install pyserial 1>/dev/null

sudo systemctl stop nvgetty
sudo systemctl disable nvgetty
sudo usermod -aG dialout shuaihu
sed -i '$a\python3 -u bikemanager/ebike.pyc &' ${HOME}/.profile
sudo reboot
